package utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import uci.utils.files.FileParserException;

public class CompareCsv
{
	protected String[] listA;
	protected String[] listB;
	protected String dataPathA;
	protected String dataPathB;
	protected double range;
	protected static Double max = -Double.MAX_VALUE;
	protected static Double min = Double.MIN_VALUE;

	public CompareCsv(String dataPathA, String dataPathB, String[] listA, String[] listB, double range)
	{
		this.dataPathA = dataPathA;
		this.dataPathB = dataPathB;
		this.listA = listA;
		this.listB = listB;
		this.range = range;
	}

	public static void main(String[] args) throws FileParserException
	{
		testOneToOne();
	}

	private static void testOneToOne()
	{
		String dataPathA = "";
		String dataPathB = "";

		// String[] listA = new
		// String[]{"C:/data/R1/matrix/ccr_2012.05.10/Statistics_20120526.10331/"
		// +
		// "Result_20120526.10481/" +
		// "RawData_MD.csv"};
		//
		//
		// String[] listB = new
		// String[]{"C:/data/Statistics_20120526.13321/Result_20120525.17191/" +
		// "RawData_MD.csv"};
		// double range= 0.000001;
		File dir = new File("C:/data/R1/matrix/ccr/2013/20130703-pioppi.orig/");

		String fileName[] = dir.list();
		for (String f : fileName)
		{
			if (!f.contains(".csv") && !f.contains(".txt"))
				continue;
			System.out.println(f);

			// String[] listB = new
			// String[]{"C:/data/R1/matrix/dbMarketDownload_20120727/2004/20040115/"
			// + f};
			String[] listA = new String[] { "C:/data/R1/matrix/ccr/2013/20130703-pioppi.orig/" + f };
			// String[] listA = new String[]{"C:/data/20040115_QA2/" + f};

			String[] listB = new String[] { "C:/data/R1/matrix/ccr/2013/20130703-pioppi.10000/" + f };
			double range = 1e-14;
			CompareCsv compareCsv = new CompareCsv(dataPathA, dataPathB, listA, listB, range);
			compareCsv.process();
			System.out.println("");
		}

		System.out.println("END");

		System.out.println("MAX=" + max);
		System.out.println("MIN=" + min);
	}

	private void process()
	{
		boolean fail = false;
		int skipRunNumberColumn = -1;
		for (int i = 0; i < listA.length; i++)
		{
			String fileA = dataPathA + listA[i];
			String fileB = dataPathB + listB[i];
			ArrayList<String> fileAList = readTxtFileToArray(fileA);
			ArrayList<String> fileBList = readTxtFileToArray(fileB);
			// System.out.println(listA[i]);
			fail = false;
			double maxDif = 0;
			if (fileAList.size() != fileBList.size())
			{
				System.out.println(listA[i] + " & " + listB[i] + " Has different Size");
				fail = true;
			}
			else
			{

				for (int j = 0; j < fileAList.size(); j++)
				{
					// System.out.println(j);
					String partsA[] = fileAList.get(j).split(" |,|\t");
					String partsB[] = fileBList.get(j).split(" |,|\t");
					if (partsA.length != partsB.length)
					{
						System.out.println("the " + listA[i] + " row[" + j + "] has different amount of elements then  " + listB[i]);
						fail = true;
					}
					if (false)
					{
						// System.out.println("Skip "+partsA[0]+" "+partsA[1]+" "+partsA[2]+" ");
						// System.out.println("Skip "+partsB[0]+" "+partsB[1]+" "+partsB[2]+" ");
						// continue;
					}
					else
					{
						for (int k = 0; k < partsA.length; k++)
						{
							try
							{
								Double valueA = Double.parseDouble(partsA[k]);
								Double valueB = Double.parseDouble(partsB[k]);
								double diff = (valueA - valueB) / (valueA != 0 ? valueA : 1);

								if (diff > max)
									max = diff;
								if (diff < min)
									min = diff;
								if (Math.abs(diff) < range)
								{
									continue;
								}
								else
								{
									if (maxDif<diff)
										maxDif=diff;
									if (k != skipRunNumberColumn)
									{

										//System.out.println("the element of file" + listA[i] + " row[" + j + "] col[" + k + "] has a dif = " + diff + " from the same element of " + listB[i]);
										fail = true;
									}
								}
							}
							catch (Exception e)
							{
								if (partsA[k].equalsIgnoreCase("RUNNUMBER"))
									skipRunNumberColumn = k;
								if (partsA[k].equalsIgnoreCase(partsB[k]))
								{
									continue;
								}
								else
								{
									//System.out.println("the element of file" + listA[i] + " row[" + j + "] col[" + k + "] = '" + partsA[k] + "' id different from '" + partsB[k] + "' of " + listB[i]);

									fail = true;
								}
							}
						}
					}
				}
			}

			if (!fail)
			{
				System.out.println(listA[i] + " AND");
				System.out.println(listB[i] + " are Equal");

			}
			if(maxDif>0)	
				System.out.println("maxDiff:" + maxDif+"  of file"  +listA[i] + " and  file:" + listB[i]);
		}
	}

	public synchronized static ArrayList<String> readTxtFileToArray(String file)
	{

		ArrayList<String> lines = null;
		BufferedReader bufRdr = null;
		try
		{
			lines = new ArrayList<String>();
			bufRdr = new BufferedReader(new FileReader(file));
			String line = null;
			while ((line = bufRdr.readLine()) != null)
			{
				lines.add(line);
			}
			bufRdr.close();
		}
		catch (FileNotFoundException e)
		{
			System.out.println(file + " not found " + e);
			// e.printStackTrace();
		}
		catch (IOException e)
		{
			System.out.println(file + " not found " + e);
			// e.printStackTrace();
		}

		return lines;
	}

}
